package edu.ncst.baseweb.service.impl.borrow;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.BorrowRecord;
import edu.ncst.baseweb.dao.equ.Equ;
import edu.ncst.baseweb.dto.JwtUser;
import edu.ncst.baseweb.mapper.BorrowRecordMapper;
import edu.ncst.baseweb.mapper.EquClassMapper;
import edu.ncst.baseweb.mapper.EquMapper;
import edu.ncst.baseweb.mapper.UserMapper;
import edu.ncst.baseweb.service.intrer.borrow.BorrowRecordService;
import edu.ncst.baseweb.utils.GetUserinfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BorrrowRecordServiceImpl implements BorrowRecordService {
    @Autowired private BorrowRecordMapper borrowRecordMapper;

    @Autowired private EquMapper equMapper;

    @Autowired private EquClassMapper equClassMapper;

    @Autowired private UserMapper userMapper;

    @Autowired
    private GetUserinfoUtil getUserinfoUtil;


    public Result<IPage<BorrowRecord>> list(BorrowRecord borrowrecord){
        QueryWrapper<BorrowRecord> queryWrapper = Wrappers.query();


        if (StringUtils.isNotEmpty(borrowrecord.getSortField()) &&StringUtils.isNotEmpty(borrowrecord.getSortOrder())) {
            String fieldName;
            if ("applyTime".equals(borrowrecord.getSortField())) {
                fieldName = BorrowRecord.COL_APPLY_TIME;
            } else {
                fieldName = "applyTime";
            }
            queryWrapper.orderBy(true, borrowrecord.getSortOrder().contains("asc"), fieldName);
        }else {
            queryWrapper.orderBy(true, true, "id");
        }
        System.out.println(borrowrecord);

        //用名称和数据库中设备名称匹配
        if(StringUtils.isNotEmpty(borrowrecord.getEquName())){
            queryWrapper.like(BorrowRecord.COL_EQU_NAME,borrowrecord.getEquName());
        }

        //获取当前登录账号的id
        JwtUser jwtUser = getUserinfoUtil.getUser();
        Integer userid = userMapper.selectIdByUserAccount(jwtUser.getUsername());
        queryWrapper.eq("user_id",userid);


        Page<BorrowRecord> userPage = new Page<>(borrowrecord.getCurrent(), borrowrecord.getPageSize());
        IPage<BorrowRecord> borrowRecordPage = borrowRecordMapper.selectPage(userPage, queryWrapper);

        //将设备表中的相关数据拿到
        List<BorrowRecord> records = borrowRecordPage.getRecords();
        for (BorrowRecord record : records){
            Equ equ = borrowRecordMapper.getEquByEquId(record.getEquId());

            String className = equClassMapper.findClassNameByClassId(equ.getClassId());
            record.setClassName(className);

            record.setEquCode(equ.getEquCode());
            record.setEquName(equ.getName());
            record.setPlace(equ.getPlace());
            record.setSpecification(equ.getSpecification());
            record.setTparameter(equ.getTparameter());
            record.setCparameter(equ.getCparameter());

            record.setClassName(className);
        }

        return Result.ok(borrowRecordPage);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    //归还
    public Result<Object> repay(BorrowRecord borrowRecord) throws Exception {
        //将设备状态该为在库中，记录状态改为2
        borrowRecordMapper.updateStateById(borrowRecord.getId(),2);
        equMapper.updateEquStateById(borrowRecord.getEquId(), "在库中");
        return Result.failure(200, "成功！");
    }
}
